package phone.qyg.com.qyg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.JSONTool;
import com.app.utils.PromptManager;
import com.app.utils.SharePrefUtil;
import com.app.utils.TransformController;
import com.app.xutils.MyCallBack;
import com.app.xutils.Xutils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import phone.qyg.com.qyg.MainActivity;
import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Third;


/**
 * 作者：lblbh on 2017/5/18 16:29
 * 邮箱：lanbuhan@163.com
 * 功能：已有账号
 */
@ContentView(R.layout.third_one)
public class ThirdOneActivity extends BaseActivity implements TextWatcher {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.login)
    TextView login;

    @ViewInject(R.id.phone)
    EditText phone;

    @ViewInject(R.id.password)
    EditText password;

    Third third;

    @Override
    public void initSucessView() {
        third = (Third) getTransModels();
    }

    @Event(value = {R.id.login,R.id.regis,R.id.forget_pas,R.id.back,R.id.weixin,R.id.qq})
    private void viewOnClick(View v){
        switch (v.getId()) {
            case R.id.login:
                String phoneStr = phone.getText().toString().trim();
                String passwordStr = password.getText().toString().trim();

                int length = passwordStr.length();
                if(length<6 || length>16){
                    PromptManager.showToast(R.string.password_number_hint);
                    return;
                }

                if(!isRequestStr(phoneStr) || !isRequestStr(passwordStr)){
                    PromptManager.showToast(R.string.phone_password_no);
                }else{
                    getLogin(phoneStr,passwordStr);
                }
                break;
            case R.id.back:
                this.finish();
                break;
        }
    }


    private void getLogin(String phone,String password){
        /*
         “mobile”:”18701581924”      //手机号
        “password”:”123456”       //密码

         */
        third.login_name = phone;
        third.password = password;
        String dataValue = JSON.toJSONString(third);
        mapKeys.put(AppHttpKey.DATA,dataValue);

        Xutils.Get(ApplicationConfig.NET_DSN_PATH + ApplicationConfig.THIRD_LOGIN_BIND,
                mapKeys,
                new MyCallBack<String>(){
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);
                        if(JSONTool.isStatus(result)){
                            loginSucess(result);
                        }else{
                            //message
                            PromptManager.showToast(JSONTool.errorHint(result));
//                            PromptManager.showToast(JSONTool.requestJSONfindName(result,JSONTool.Enum.MESSAGE));
                        }
                    }
                });
    }


    /**
     * 登录成功
     * @param result
     */
    private void loginSucess(String result){

        SharePrefUtil.saveString(SharePrefUtil.KEY.APP_USER, JSONTool.requestJSONStringKey(result, JSONTool.Enum.DATA));

        Intent intent = new Intent(ApplicationConfig.APP_QX_LOGIN);
        intent.putExtra(TransformController.ChangeKEY.INTENT_KEY,result);
        LocalBroadcastManager.getInstance(QXApplication.getContext()).sendBroadcast(intent);

        ThirdOneActivity.this.finish();
        TransformController.transformController(QXApplication.getContext(), MainActivity.class);
    }


    @Override
    public void initData(Bundle savedInstanceState) {}

    @Override
    public void initViewHandler() {
        phone.addTextChangedListener(this);
        password.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        login.setEnabled(false);
        login.setBackgroundResource(R.drawable.ash_btn_frame);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String phoneStr = phone.getText().toString().trim();
        String passStr = password.getText().toString().trim();

        if(isRequestStr(phoneStr)&&isRequestStr(passStr)){
            login.setEnabled(true);
            login.setBackgroundResource(R.drawable.blue_btn_frame);
        }else{
            login.setEnabled(false);
            login.setBackgroundResource(R.drawable.ash_btn_frame);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
